package com.zoe.app.listener;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zoe.app.event.CommonEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author Zoe.Zhang
 * @date 2021/10/28
 * @description 使用注解的方式监听
 */
@Component
public class CommonEventListener {
    private static final Logger logger = LoggerFactory.getLogger(CommonEventListener.class);

    @EventListener(classes = {CommonEvent.class})
    public void onApplicationEvent(CommonEvent commonEvent) {
        // 获取发送事件的类
        Object source = commonEvent.getSource();
        DateTime date = DateUtil.date(commonEvent.getTimestamp());
        logger.info("CommonEvent");
        logger.error(source.toString());
        logger.error(date.toString());
        // 获取发送事件时候绑定的消息
        String message = commonEvent.getMessage();
        logger.error(message);
    }
}
